449B - Jzzhu and Cities - CodeForces Solution


graphs greedy shortest paths *2000

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
#pragma GCC optimize "trapv"
using namespace std ;

const long double EPS = 1e-9 ;
const int MOD=1e9+7 ;
const int N = 4e5 + 5  ;
const int SZ = 2e5 ;
const long long inf = 2e15 ;

#define GOMO_GOMO_NOO ios_base::sync_with_stdio(0),cin.tie(0) , cout.tie(0) ;
#define ll long long
#define endl '\n'
#define all(v) v.begin() , v.end()
int ans = 0 ;
ll dis[N] ;

int main() {

    GOMO_GOMO_NOO
    int n , m , k ;
    cin >> n >> m >> k ;
    vector<vector<pair<int , pair<int , bool >>>> country(n + 1 ) ;
    vector<bool> close(n + 1 , 0 ) ;
    for (int u , v , w , i = 0; i < m ; ++i) {
        cin >> u >> v >> w ;
        country[u].push_back({v , {w , 0 } }) ;
        country[v].push_back({u , {w , 0 } }) ;
    }
    vector<int> train ;
    for (int i = 0; i < k; ++i) {
        int x , y ;
        cin >> x >> y ;
        train.push_back(x) ;
        country[1].push_back({x , {y , 1 }}) ;
        country[x].push_back({1 , {y , 1 }}) ;
    }

    fill(dis , dis + N , inf) ;
    set<pair<ll , pair<int , bool >>> st ;
    st.insert({0 , {1 , 0} }) ;
    dis[1] = 0 ;
    while (!st.empty()){
        ll cost = st.begin()->first ;
        int node = st.begin()->second.first ;

        st.erase(st.begin()) ;
        if(cost > dis[node]){
            continue;
        }
        for(auto i : country[node]){
            ll total_cost = cost + i.second.first ;
            int used = i.second.second ;
            if(total_cost < dis[i.first]){
                dis[i.first] = total_cost ;
                st.insert({total_cost , {i.first , i.second.second }}) ;
                close[i.first] = i.second.second ;
            }else if(!used && dis[i.first] == total_cost){
                close[i.first] = 0 ;
            }
        }
    }
    int cnt = count(all(close) , 1 ) ;
    cout << k - cnt << endl ;
}


Comments

Submit
0 Comments
More Questions

455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters